package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository repository;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyComment comment) {
        comment.setId(null);
        repository.save(comment);

    }

    @Override
    public void update(StrategyComment comment) {
        repository.save(comment);
    }

    @Override
    public StrategyComment get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {

        // 查询对象
        Query query = new Query();
        // 查询条件
        if (StringUtils.hasLength(qo.getStrategyId())) {
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "createTime");
        // 调用分页工具类
        return DBHelper.query(template,StrategyComment.class,query,pageable);
    }



    @Override
    public void addComment(StrategyComment comment,String strategyId, UserInfo userInfo) {
        // 维护数据
        // userId   评论用户的id即当前登录用户
        comment.setUserId(userInfo.getId());
        // nickName , headImgUrl , city , leve   使用BeanUtils工具类,复杂当前登录用户中的属性(只能复制相同的属性)
        BeanUtils.copyProperties(userInfo,comment);
        // createTime   评论时间为当前时间
        comment.setCreateTime(new Date());
        // 该攻略下的评论数 + 1
//        Strategy strategy = strategyService.get(strategyId);
//        strategy.setReplynum(strategy.getReplynum() + 1);
//        strategyService.update(strategy);
        this.save(comment);

    }



    @Override
    public void addThumb(String cid, String sid, String uid) {
        // 获取被点赞/被取消点赞的评论
        StrategyComment comment = this.get(cid);
        comment.setStrategyId(sid);
        List<String> thumbuplist = comment.getThumbuplist();
        if (thumbuplist.contains(uid)) {    // 如果当前登录用户id存在集合中,表示已点赞
            comment.setThumbupnum(comment.getThumbupnum()-1);
            thumbuplist.remove(uid);
        }else{  // 不存在则表示该用户未点赞
            comment.setThumbupnum(comment.getThumbupnum()+1);
            thumbuplist.add(uid);
        }
        this.update(comment);
    }


}
